package k5_net.demo03;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPServer {

    public static void main(String[] args) {
        try {
            //创建服务端的socket
            DatagramSocket serverSocket = new DatagramSocket(9999);
            System.out.println("服务器已经准备好了");


            //创建一个缓冲字节数组，把读取到的数据依次通过字节数组读取显示
            byte[] byteBuffered = new byte[1024];
            DatagramPacket packet = new DatagramPacket(byteBuffered,byteBuffered.length);

            while(true){
                //接收其他任何地方向此端口发送来的数据报
                serverSocket.receive(packet);
                System.out.println("接收到的数据："+new String(byteBuffered,0, packet.getLength()));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
